//
//  UIView+MAAdv.m
//  MateAdv
//
//  Created by 茶 on 2023/9/8.
//

#import "UIView+MAAdv.h"
#import "Aspects.h"

@implementation UIView (MAAdv)
#pragma mark - 设置部分圆角
- (void)ma_addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii {
    
    [self _ma_addRoundedCorners:corners withRadii:radii];
    
    MAWeakify(self);
    [self aspect_hookSelector:@selector(layoutSubviews) withOptions:AspectPositionAfter usingBlock:^{
        MAStrongify(self);
        [self _ma_addRoundedCorners:corners withRadii:radii];
    } error:nil];
    
}

- (void)ma_addRoundedCorners:(UIRectCorner)corners withRadiif:(CGFloat)radiif {
    
    [self _ma_addRoundedCorners:corners withRadii:CGSizeMake(radiif, radiif)];
    MAWeakify(self);
    [self aspect_hookSelector:@selector(layoutSubviews) withOptions:AspectPositionAfter usingBlock:^{
        MAStrongify(self);
        [self _ma_addRoundedCorners:corners withRadii:CGSizeMake(radiif, radiif)];
    } error:nil];
}

- (void)ma_addRoundedCorners:(UIRectCorner)corners withRadiif:(CGFloat)radiif borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor {
    [self _ma_addRoundedCorners:corners withRadii:CGSizeMake(radiif, radiif) borderWidth:borderWidth borderColor:borderColor];
    MAWeakify(self);
    [self aspect_hookSelector:@selector(layoutSubviews) withOptions:AspectPositionAfter usingBlock:^{
        MAStrongify(self);
        [self _ma_addRoundedCorners:corners withRadii:CGSizeMake(radiif, radiif) borderWidth:borderWidth borderColor:borderColor];
    } error:nil];
}

- (void)_ma_addRoundedCorners:(UIRectCorner)corners
                    withRadii:(CGSize)radii
                  borderWidth:(CGFloat)borderWidth
                  borderColor:(UIColor *)borderColor {
    
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = rounded.CGPath;
    
    CGRect maskRect = CGRectMake(borderWidth, borderWidth, self.bounds.size.width-borderWidth*2, self.bounds.size.height-borderWidth*2);
    UIBezierPath *rounded1 = [UIBezierPath bezierPathWithRoundedRect:maskRect byRoundingCorners:corners cornerRadii:CGSizeMake(radii.width, radii.height)];
    CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
    borderLayer.lineWidth = borderWidth;
    borderLayer.strokeColor = borderColor.CGColor;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    [borderLayer setPath:rounded1.CGPath];
    [self.layer insertSublayer:borderLayer atIndex:0];
    
    self.layer.mask = maskLayer;
}
    
- (void)_ma_addRoundedCorners:(UIRectCorner)corners
                    withRadii:(CGSize)radii {
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
    
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    
    self.layer.mask = shape;
}

- (void)setMa_cornerRadius:(CGFloat)ma_cornerRadius {
    [self ma_addRoundedCorners:UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight withRadii:CGSizeMake(ma_cornerRadius, ma_cornerRadius)];
}

- (CGFloat)ma_cornerRadius {
    return 0;
}

@end

@implementation MASConstraint (MAAdv)

- (MASConstraint *(^)(CGFloat))ma_offset{
    return ^id(CGFloat offset){
        self.offset = MARatioWidth(offset);
        return self;
    };
}

- (MASConstraint *(^)(CGFloat))ma_h_offset{
    return ^id(CGFloat offset){
        self.offset = MARatioHeight(offset);
        return self;
    };
}

@end
